<?php

	class Youtube_API3 {
		protected $_youtubeService = false;

		function __construct() {
			$client = new Google_Client();
			$client->setClientId( YT_OAUTH2_CLIENT_ID );
			$client->setClientSecret( YT_OAUTH2_CLIENT_SECRET );
			$client->setAccessType( 'offline' );
			$client->refreshToken( YT_OAUTH2_REFRESH_TOKEN );
			$client->setAccessToken( $client->getAccessToken() );
			
			// YouTube object used to make all API requests.
			$this->_youtubeService = new Google_YoutubeService( $client );
		}
	
		public function getAllChannels() {
			return $this->_youtubeService->channels->listChannels( 'brandingSettings,contentDetails', array( 'mine' => true ));
		}
		
		public function getChannelDetails( $channelId ) {
			return $this->_youtubeService->channels->listChannels( 'brandingSettings,contentDetails', array( 'id' => $channelId ));
		}
		
		public function upload( $title, $slug, $description, $category, $tags, $videoPath, $mineType, $channelId, $videoStatus = 'private' ) {
			try{
				// Create a snipet with title, description, tags and category id
				$snippet = new Google_VideoSnippet();
				$snippet->setTitle( $title );
				$snippet->setDescription( $description );
				$snippet->setTags( explode( ',', $tags ) );
				
				// Numeric video category. See
				// https://developers.google.com/youtube/v3/docs/videoCategories/list
				$snippet->setCategoryId( $category );
				
				//$snippet->setChannelId( $channelId );
				
				//video status unlisted for current launch
				$videoStatus = 'unlisted';
				
				// Create a video status with privacy status. Options are "public", "private" and "unlisted".
				$status = new Google_VideoStatus();
				$status->privacyStatus = $videoStatus;
				
				// Create a YouTube video with snippet and status
				$video = new Google_Video();
				$video->setSnippet($snippet);
				$video->setStatus($status);
				
				// Size of each chunk of data in bytes. Setting it higher leads faster upload (less chunks,
				// for reliable connections). Setting it lower leads better recovery (fine-grained chunks)
				$chunkSizeBytes = 1 * 1024 * 1024;
				
				// Create a MediaFileUpload with resumable uploads
				$media = new Google_MediaFileUpload('video/*', null, true, $chunkSizeBytes);
				$media->setFileSize(filesize($videoPath));
				
				// Create a video insert request
				$insertResponse = $this->_youtubeService->videos->insert("status,snippet", $video,
				    array('mediaUpload' => $media));
				
				$uploadStatus = false;
				
				// Read file and upload chunk by chunk
				$handle = fopen($videoPath, "rb");
				while (!$uploadStatus && !feof($handle)) {
					
				  $chunk = fread($handle, $chunkSizeBytes);
				  $uploadStatus = $media->nextChunk($insertResponse, $chunk);
				}
				
				fclose($handle);
				
				return $uploadStatus;
			
			} catch (Google_ServiceException $e) {
				throw $e;
			} catch (Google_Exception $e) {
				throw $e;
			}
		}

		function updateVideoPrivacy( $videoId, $privacy = 'public' ) {
			$listResponse = $this->_youtubeService->videos->listVideos( 'snippet',
			array( 'id' => $videoId ) );
			$videoList = $listResponse[ 'items' ];
			if( empty( $videoList ) ) {
				return false;
			} else {
				$video = $videoList[ 0 ];
				$videoSnippet = $video[ 'snippet' ];
				
				$status = new Google_VideoStatus();
				$status->privacyStatus = $privacy;

				$updateVideo = new Google_Video( $video );
				$updateSnippet = new Google_VideoSnippet( $videoSnippet );
				$updateVideo->setStatus( $status );
				$updateVideo -> setSnippet($updateSnippet);

				try {
					$this->_youtubeService->videos->update( 'snippet,status', $updateVideo );
					return true;
				} catch( Exception $ex ) {
					return false;
				}
			}
		}
	}
	
	